package com.ck.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.security.providers.encoding.Md5PasswordEncoder;

import com.ck.dao.AdminDAO;
import com.ck.domain.Admin;
import com.ck.domain.Teacher;
import com.ck.service.AdminService;

public class AdminServiceImpl implements AdminService {
	
	private AdminDAO adminDAO;
	
	private Admin admin;
	private Teacher teacher;

	public void setAdminDAO(AdminDAO adminDAO) {
		this.adminDAO = adminDAO;
	}
	
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public List<Admin> gainAdminList(){
		return adminDAO.getAllAdmin();
	}
	
	public Admin gainAdmin(String username) {
		return adminDAO.getAdminByUsername(username);
	}

	public Admin gainAdmin(Integer id) {
		return adminDAO.getAdminById(id);
	}

	public boolean addNewAdmin(Admin admin, Map model) {
		Admin dbAdmin = adminDAO.getAdminByUsername(admin.getUsername());
		if(dbAdmin == null){
			Date now = new Date();
			admin.setCst_create(now);
			admin.setCst_modify(now);
			admin.setPassword(md5("123456"));
			admin.setSuperadmin(0);
			
			adminDAO.addNewAdmin(admin);
			return true;
		} else {
            model.put("error_username", "该帐号已经存在！");
            return false;
        }
	}
	
	public void deleteAdmin(Integer id){
		adminDAO.deleteAdminById(id);
	}
	
	public boolean editAdminOwnInfo(Admin admin) {
		admin.setCst_modify(new Date());
		adminDAO.updateAdminOwnInfo(admin);
		return true;
	}

	public boolean validatePassword(Admin admin, String confirmPassword,
			Map model) {
		if(confirmPassword.equals(admin.getPassword())){
			return true;
		} else {
            model.put("error_confirmPassword", "两次密码输入不一致！");
            return false;
        }
	}
	
	private Admin md5(Admin admin) {
        Md5PasswordEncoder md5 = new Md5PasswordEncoder();
        admin.setPassword(md5.encodePassword(admin.getPassword(), new String(
                "canyouguessme")));
        return admin;
    }

    private String md5(String password) {
        Md5PasswordEncoder md5 = new Md5PasswordEncoder();
        password = md5.encodePassword(password, new String("canyouguessme"));
        return password;
    }

	public boolean editAdminOwnPassword(String oldPassword, Admin admin,
			Map model) {
		Admin dbAdmin = adminDAO.getAdminById(admin.getId());
		if(dbAdmin.getPassword().equals(md5(oldPassword))){
			md5(admin);
			admin.setCst_modify(new Date());
			adminDAO.updateAdminOwnPassword(admin);
			return true;
		}else{
			model.put("error_oldPassword", "原密码不正确！");
            return false;
		}
	}

	public boolean editAdminInfo(Admin admin) {
		admin.setCst_modify(new Date());
		adminDAO.updateAdminInfo(admin);
		return true;
	}

	public void resetAdminPassword(Integer id) {
		admin.setId(id);
		admin.setCst_modify(new Date());
		admin.setPassword(md5("123456"));
		adminDAO.updateAdminPassword(admin);
	}

	public List<Teacher> gainTeacherList(){
		return adminDAO.getAllTeacher();
	}
	
	public Teacher gainTeacher(Integer id){
		return adminDAO.getTeacherById(id);
	}
	
	public Teacher gainTeacher(String username) {
		return adminDAO.getTeacherByUsername(username);
	}

	public boolean addNewTeacher(Teacher teacher, Map model) {
		Teacher dbTeacher = adminDAO.getTeacherByUsername(teacher.getUsername());
		if(dbTeacher == null){
			Date now = new Date();
			teacher.setCst_create(now);
			teacher.setCst_modify(now);
			teacher.setPassword(md5("123456"));
			
			adminDAO.addNewTeacher(teacher);
			return true;
		} else {
            model.put("error_username", "该帐号已经存在！");
            return false;
        }
	}

	public void deleteTeacher(Integer id) {
		adminDAO.deleteTeacherById(id);
	}

	public boolean editTeacherInfo(Teacher teacher) {
		teacher.setCst_modify(new Date());
		adminDAO.updateTeacherInfo(teacher);
		return true;
	}

	public void resetTeacherPassword(Integer id) {
		teacher.setId(id);
		teacher.setCst_modify(new Date());
		teacher.setPassword(md5("123456"));
		adminDAO.updateTeacherPassword(teacher);
	}

	

}
